﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Utils;
using HitachiLib.Data;
using HitachiLib.HitachiServiceRef;
using HitachiApp.Model.Utils;

namespace HitachiApp.Model.Projects
{
    public class ProjectManageItem : INotifyPropertyChanged
    {
        private bool selected = false;
        public bool Selected
        {
            get { return selected; }
            set {
                selected = value;
                RaisePropertyChanged("Selected");
            }
        }

        private Project data;
        public Project Data
        {
            get { return data; }
            set { data = value; }
        }
        private SortableObservableCollection<ProjectManageItem> children = new SortableObservableCollection<ProjectManageItem>();
        private ProjectManageItem parent = null;
        public SortableObservableCollection<ProjectManageItem> Children
        {
            get { return children; }
            set { children = value; }
        }
        public ProjectManageItem Parent
        {
            get { return parent; }
            set { parent = value; }
        }
        public String Name
        {
            get
            {
                if (data.Data.Name != null && !data.Data.Name.Equals(""))
                    return data.Data.Name;
                else
                    return data.Data.CustomName + " - " + data.Data.DevID;
            }
        }
        public Visibility DeviceVisibility
        {
            get
            {
                if (data.Data.DevID == null)
                    return Visibility.Collapsed;
                else if (data.Data.DevID.Length == 0)
                    return Visibility.Collapsed;
                else
                    return Visibility.Visible;
            }
        }
        public Visibility RouteVisibility
        {
            get
            {
                if (DeviceVisibility == Visibility.Collapsed)
                    return Visibility.Visible;
                else
                    return Visibility.Collapsed;
            }
        }

        public void ClearSelected()
        {
            Selected = false;
            foreach (ProjectManageItem pmi in children)
            {
                pmi.ClearSelected();
            }
            RaisePropertyChanged("Selected");
        }

        public static int CompareByDevID(ProjectManageItem a, ProjectManageItem b)
        {
            if (a.Data.Data.DevID != null && b.Data.Data.DevID != null)
                return a.Data.Data.DevID.CompareTo(b.Data.Data.DevID);
            if (a.Data.Data.DevID != null)
                return -1;
            if (a.Data.Data.DevID != null)
                return 1;
            if (a.Data.Data.Name != null && b.Data.Data.Name != null)
                return a.Data.Data.Name.CompareTo(b.Data.Data.Name);
            if (a.Data.Data.Name != null)
                return -1;
            if (a.Data.Data.Name != null)
                return 1;
            if (a.Data.Data.CustomName != null && b.Data.Data.CustomName != null)
                return a.Data.Data.CustomName.CompareTo(b.Data.Data.CustomName);
            if (a.Data.Data.CustomName != null)
                return -1;
            if (a.Data.Data.CustomName != null)
                return 1;
            return 0;
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
